package com.offcn.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.offcn.pojo.Setmeal;
import com.offcn.service.SetmealService;
import com.offcn.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * @Author: wy
 * @Date: 2022/8/30 16:29
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    private Result result;

    @Reference
    private SetmealService setmealService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //文件上传
    @RequestMapping("/uploadpic")
    public Result uploadpic(@RequestParam(value = "imgFile") MultipartFile multipartFile) {
        /*
          //获取文件名称
            String fileName = photo.getOriginalFilename();

            System.out.println("文件名称为：" + fileName);

            fileName = UUID.randomUUID() + "_" + fileName;

            //指定上传文件地址
            File file = new File("/Users/lazy-bay/Desktop/filename/" + fileName);

            //文件上传的方法
            photo.transferTo(file);

            model.addAttribute("fileName", fileName);
         */

        try {
            String fimleName = FileUploadUtils.uploadPic(multipartFile);

            //存放 无效 和 有 效的照片
            SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
            opsForSet.add(RedisConstant.SETMEAL_PIC_UPLOAD,fimleName);

            result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fimleName);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.PIC_UPLOAD_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }

    //添加新增套餐
    @RequestMapping("/addSetmeal")
    public Result addSetmeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        System.out.println(Arrays.toString(checkgroupIds));
        System.out.println(setmeal);

        try {
            setmealService.addSetmeal(setmeal, checkgroupIds);

            //存放有效 照片 到 dbpic 集合中
            SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
            opsForSet.add(RedisConstant.SETMEAL_PIC_DB,setmeal.getImg());

            result = new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS, null);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.ADD_SETMEAL_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    //修改编辑
    @RequestMapping("/editSetmeal")
    public Result editSetmeal(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.editSetmeal(setmeal, checkgroupIds);

            //修改的时候 我们 吧照片 放进有效照片 dbpic集合中
            SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
            opsForSet.add(RedisConstant.SETMEAL_PIC_DB,setmeal.getImg());


            result = new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS, null);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.EDIT_SETMEAL_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }


    //删除
    @RequestMapping("/deleteInfo")
    public Result deleteInfo(@RequestParam(value = "id") Integer id) {
        try {
            result = setmealService.deleteInfo(id);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.DELETE_SETMEAL_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }
}
